package timor.timor;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import autoview.MyGridView;
import datas.Goodsclass;


public class SpeciesActivity extends ActionBarActivity {
private Gridviewadapter gridviewadapter;
private MyGridView myGridView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_species);
        myGridView= (MyGridView) findViewById(R.id.spegridview);
        goods=new ArrayList<Goodsclass>();
        gridviewadapter=new Gridviewadapter();
        myGridView.setAdapter(gridviewadapter);
        bitmapUtils=new BitmapUtils(this);
        httpUtils=new HttpUtils();
        getSupportActionBar().hide();
        Intent intent=getIntent();
        String url=intent.getStringExtra("target_url");
        getGridviewadte(url);
    }

private ArrayList<Goodsclass> goods;
private BitmapUtils bitmapUtils;
private HttpUtils httpUtils;
    //gridview适配器
    class Gridviewadapter extends BaseAdapter {

        @Override
        public int getCount() {
            return goods.size();
        }

        @Override
        public Goodsclass getItem(int position) {
            return goods.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView==null){
                holder=new ViewHolder();
                convertView= LayoutInflater.from(SpeciesActivity.this).inflate(R.layout.homemygridview,null);
                holder.imageView= (ImageView) convertView.findViewById(R.id.gridimg);
                holder.textView1= (TextView) convertView.findViewById(R.id.xianjia);
                holder.textView2= (TextView) convertView.findViewById(R.id.yuanjia);
                holder.textView3= (TextView) convertView.findViewById(R.id.zhekuo);
                holder.textView4= (TextView) convertView.findViewById(R.id.gridtitle);
                convertView.setTag(holder);
            }
            else {
                holder= (ViewHolder) convertView.getTag();
            }
            Goodsclass goodsclass=goods.get(position);
            bitmapUtils.display(holder.imageView,goodsclass.img);
            holder.textView1.setText("¥" + goodsclass.newprice + " ");
            holder.textView2.setText("¥"+goodsclass.oldprice+" ");
            holder.textView3.setText(goodsclass.discount+"折");
            holder.textView4.setText(goodsclass.title);
            return convertView;
        }
        class ViewHolder{
            ImageView imageView;
            TextView textView1;
            TextView textView2;
            TextView textView3;
            TextView textView4;
        }
    }
//gridview数据
    private void getGridviewadte(String url){

        httpUtils.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    JSONObject jsonob=new JSONObject(responseInfo.result);
                    JSONArray jsona=jsonob.getJSONArray("goods");
                    for (int i = 0; i <jsona.length(); i++) {
                        JSONObject jsonbb=jsona.getJSONObject(i);
                        JSONObject jsonimag=jsonbb.getJSONObject("imgs");
                        Goodsclass good=new Goodsclass();
                        good.img=jsonimag.getString("big");
                        good.newprice=jsonbb.getString("now_price");
                        good.oldprice=jsonbb.getString("origin_price");
                        good.discount=jsonbb.getString("discount");
                        good.title=jsonbb.getString("title");
                        goods.add(good);
                    }
                    gridviewadapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(HttpException e, String s) {
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_species, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
